#define NUM_OCTAVES 5
precision mediump float;
attribute vec4 a_position;
uniform mat4 u_formMatrix;
attribute vec3 a_Normal;
varying vec3 v_Normal;
varying float v_radius;
varying vec3 v_position;
uniform float u_time;
uniform float u_roundness;
varying mat3 v_rotation;
float mod289(float x){return x - floor(x * (1.0 / 289.0)) * 289.0;}
vec4 mod289(vec4 x){return x - floor(x * (1.0 / 289.0)) * 289.0;}
vec4 perm(vec4 x){return mod289(((x * 34.0) + 1.0) * x);}
mat3 rotation3dY(float angle) {
	float s = sin(angle);
	float c = cos(angle);

	return mat3(
		c, 0.0, -s,
		0.0, 1.0, 0.0,
		s, 0.0, c
	);
}
float noise(vec3 p){
    vec3 a = floor(p);
    vec3 d = p - a;
    d = d * d * (3.0 - 2.0 * d);

    vec4 b = a.xxyy + vec4(0.0, 1.0, 0.0, 1.0);
    vec4 k1 = perm(b.xyxy);
    vec4 k2 = perm(k1.xyxy + b.zzww);

    vec4 c = k2 + a.zzzz;
    vec4 k3 = perm(c);
    vec4 k4 = perm(c + 1.0);

    vec4 o1 = fract(k3 * (1.0 / 41.0));
    vec4 o2 = fract(k4 * (1.0 / 41.0));

    vec4 o3 = o2 * d.z + o1 * (1.0 - d.z);
    vec2 o4 = o3.yw * d.x + o3.xz * (1.0 - d.x);

    return o4.y * d.y + o4.x * (1.0 - d.y);
}

float fbm(vec3 x) {
	float v = 0.0;
	float a = 0.5;
	vec3 shift = vec3(100);
	for (int i = 0; i < NUM_OCTAVES; ++i) {
		v += a * noise(x);
		x = x * 2.0 + shift;
		a *= 0.5;
	}
	return v;
}
void main(void){

 vec4 newPosition=a_position;

  float radius=1.;
  
  float noise1=fbm(u_time*.1+.6*a_position.xyz);
  float noise2=fbm(u_time*.05+2.*a_position.xyz);
  
  radius*=mix(1.-u_roundness,1.+u_roundness,noise1);
  radius*=mix(1.-u_roundness,1.+u_roundness,noise2);
   gl_Position=u_formMatrix*vec4(vec3(newPosition)*radius,1.);
  // newPosition*=0.1;
  mat3 rotation=rotation3dY(u_time*.2);
 v_position=newPosition.xyz;
 v_Normal=a_Normal;
 v_rotation=rotation;
  
}